package uni.UNIBEE7B8D.view.Study.Zhibo.ZhiboList;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import uni.UNIBEE7B8D.Adapter.NewsFragmentAdapter;
import uni.UNIBEE7B8D.BaseActivity;
import uni.UNIBEE7B8D.Enty.HomeMenu;
import uni.UNIBEE7B8D.Enty.HomeZhibo;
import uni.UNIBEE7B8D.Presenter.ZhiboListPersenter;
import uni.UNIBEE7B8D.R;

public class ZhiboListActivity extends BaseActivity implements View.OnClickListener , ZhiboListContract.ZhiboListView {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.headTitle)
    TextView headTitle;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.pst)
    TabLayout pst;
    @BindView(R.id.pst_view)
    LinearLayout pstView;
    @BindView(R.id.vp)
    ViewPager vp;

    private List<String> title;
    private List<Fragment> fragmentList;
    private NewsFragmentAdapter nfa;

    private ZhiboListPersenter zhiboListPersenter = new ZhiboListPersenter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhibo_list);
        ButterKnife.bind(this);


        back.setOnClickListener(this);
        title = new ArrayList<>();
        fragmentList = new ArrayList<>();

        //导航
        zhiboListPersenter.getList();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    public void Toast(String s) {

    }

    @Override
    public void GetMeny(HomeMenu homeMenu) {
        List<HomeMenu.ResultBean.RecordsBean> list = new ArrayList<>();
        list = homeMenu.getResult().getRecords();
        pst.addTab(pst.newTab().setText("首页"));
        title.add("首页");
        for (HomeMenu.ResultBean.RecordsBean item:list) {
            title.add(item.getName());
        }
        for (int i = 0; i < title.size(); i++) {
            ZhiboListFragment zhiboListFragment1 = new ZhiboListFragment();
            fragmentList.add(zhiboListFragment1);
        }
        nfa = new NewsFragmentAdapter(getSupportFragmentManager(), fragmentList, title);
        //pst.setTextColor(0xE3E2E3);
        nfa.notifyDataSetChanged();
        vp.setAdapter(nfa);
        pst.setupWithViewPager(vp);
        pst.getTabAt(0).select();
        pst.getTabAt(0).setCustomView(R.layout.tab_layout_text);
        TextView textView = pst.getTabAt(0).getCustomView().findViewById(android.R.id.text1);
        textView.setTextAppearance(getBaseContext(), R.style.TabLayoutTextSize);

        pst.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(getBaseContext(), R.style.TabLayoutTextSize);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(getBaseContext(), R.style.TabLayoutUnsectedTextSize);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void GetZhibo(HomeZhibo homeZhibo) {

    }
}
